package com.bafa.front;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bafa.dbcache.LeagueCache;
import com.bafa.exceptions.ErrorException;
import com.bafa.front.presentationbeans.LeagueBean;
import com.bafa.front.presentationbeans.LeaguePositionBean;
import com.bafa.front.presentationbeans.MatchListBean;
import com.bafa.front.presentationbeans.PlayerListBean;
import com.bafa.front.presentationbeans.TeamBean;
import com.bafa.front.presentationbeans.TeamListBean;
import com.bafa.front.utils.FrontUtils;
import com.bafa.objects.AdminUser;
import com.bafa.objects.Division;
import com.bafa.objects.LeaguePosition;
import com.bafa.objects.PageBean;
import com.bafa.objects.season.Season;
import com.bafa.objects.team.Team;
import com.bafa.objects.User;
import com.bafa.services.RetrieveServices;
import com.bafa.services.LogonServices;

public class LogonServlet extends DaddyServlet {

	@Override
	public void doEvent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ErrorException {
		try{
		
		HttpSession session = request.getSession();
		User user = null;
		
		if(FrontUtils.isLoggedOn(request)){
			user = (User)FrontUtils.getUser(request);
		}else{
			if(request.getParameter("user")!=null){
				user = LogonServices.logon(request.getParameter("user"),request.getParameter("password"));
				if(request.getParameter("rememberme")!=null&&request.getParameter("rememberme").equalsIgnoreCase("true")){
					FrontUtils.rememberMeCookie(response,request.getParameter("user"));
				}
			}else{
				ErrorException error = new ErrorException();
				error.addErrorMessage("You have to logon for this function. Please logon.");
				throw error;
			}
			
		}
		
		if(user.getRights().equals("admin")){
			AdminUser admin = new AdminUser(null,user.getRights(),user.getUserId());
			pageBean = (PageBean)LeagueCache.getPages().get("admin");

			request.setAttribute("TEAMS",new TeamListBean(RetrieveServices.getAllTeams()));
			
			session.setAttribute("LOGGEDON",admin);
		
		}
		else if(user.isResetPassword()){
			session.setAttribute("LOGGEDON",user);
			LeagueBean league = (LeagueBean)session.getAttribute("DIVISION");
			TeamBean team = new TeamBean(RetrieveServices.getTeam(user.getTeamId()+""),FrontUtils.getThisSeason(request).getId());
			session.setAttribute("LOGGEDONTEAM",team);
		    PageBean pageBeanTemp = (PageBean)LeagueCache.getPages().get("resetpassword");
		    pageBean = new PageBean(pageBeanTemp);
		    pageBean.setPageheader(pageBean.getPageheader().replaceAll("<>", team.getName()));
		    pageBean.setMainheader(pageBean.getMainheader().replaceAll("<>", team.getName()));
		}else if(user.getRights().equals("team")){
			LeagueBean league = (LeagueBean)session.getAttribute("DIVISION");
			TeamBean team = new TeamBean(RetrieveServices.getTeam(user.getTeamId()+""),FrontUtils.getThisSeason(request).getId());
			LeaguePositionBean allTimeRecord = new LeaguePositionBean(user.getTeamId());
			LeaguePositionBean leagueRecord = new LeaguePositionBean(user.getTeamId(),FrontUtils.getThisSeason(request).getId());
			PlayerListBean allTimeScorers = new PlayerListBean(RetrieveServices.getTeam(user.getTeamId()+"").getStats().scorers(),team.getId());
			request.setAttribute("SCORERS",allTimeScorers);
			request.setAttribute("RECORD",allTimeRecord);
			request.setAttribute("LEAGUERECORD",leagueRecord);
			request.setAttribute("TEAM",team);
			session.setAttribute("LOGGEDONTEAM",team);
			session.setAttribute("LOGGEDON",user);
			
				PageBean pageBeanTemp = (PageBean)LeagueCache.getPages().get("user");
				pageBean = new PageBean(pageBeanTemp);
				pageBean.setPageheader(pageBean.getPageheader().replaceAll("<>", team.getName()));
                                pageBean.setMainheader(pageBean.getMainheader().replaceAll("<>", team.getName()));
				
			
		}
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute("ERROR",e);
			url = "error.jsp";
		}
		
	}
}
